package cate.game.role.res.guild;

import cate.common.table.d.GDGuild;
import cate.game.GameBody;
import cate.game.db.mongo.kv.KvMongo;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class GuildSPO extends KvMongo {
    @NoteField(value = "编号计数器")
    @Transient
    public transient AtomicInteger count;

    @NoteField(value = "生成的所有公会数量")
    public int noMax;

    @NoteField(value = "解散的公会时间戳", detail = "<公会uid,解散时间>")
    public Map<String, Long> disbandMap;

    @NoteField(value = "成员被踢出去的时间", detail = "<成员uid,解散时间>")
    public Map<String, Long> kickOutMap;


    @Override
    public String getKeyPrefix() {
        return KvMongo.KEY_GUILD_DATA;
    }

    @Override
    public void initialize() {
        if (count == null) {
            count = new AtomicInteger(noMax);
        }
        if (disbandMap == null) {
            disbandMap = new HashMap<>();
        }
        if (kickOutMap == null) {
            kickOutMap = new HashMap<>();
        }
    }

    public String newGuildNo(Role role) {
        return newGuildNoGlobal(role);
    }

    public String newGuildNo(GameBody gameBody) {
        return newGuildNoGlobal(gameBody);
    }

    private String newGuildNoGlobal(Role role) {
        return role.getGame().apis().world.gid.buildGuildNo();
    }


    private String newGuildNoGlobal(GameBody game) {
        return game.apis().world.gid.buildGuildNo();
    }

    public void onDaySpan() {
        List<String> outDate = disbandMap
                .keySet()
                .stream()
                .filter(uid -> System.currentTimeMillis() - disbandMap.get(uid) > GDGuild.basic.PUNISH_SECOND)
                .collect(Collectors.toList());
        outDate.forEach(uid -> disbandMap.remove(uid));
    }
}
